package com.xw.screen.capture.thread;

import com.alibaba.fastjson.JSON;
import com.xw.screen.capture.entity.CaptureScreenEnum;
import com.xw.screen.capture.entity.ImageInfo;
import com.xw.screen.capture.entity.MultipartFileImpl;
import com.xw.screen.capture.entity.ScreenImageInfo;
import com.xw.screen.capture.util.CaptureScreenUtils;
import com.xw.screen.capture.util.FileUpload;
import com.xw.screen.capture.util.ImageUtil;
import com.xw.screen.capture.util.OKHttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;

/**
 * @author ：xiongwei
 * @date ：Created in 2020/11/22 0:53
 */
@Slf4j
public class CaptureScreenThread extends Thread {

    /**
     * 睡眠时间
     */
    private final int sleepTime;

    /**
     * 速率
     */
    private int rete;

    /**
     * 截取图片后处理方式
     */
    private final CaptureScreenEnum screenEnum;


    /**
     * 构造睡眠时间
     *
     * @param time 时间，单位是100毫秒
     */
    public CaptureScreenThread(int time, int rete, CaptureScreenEnum screenEnum) {
        double retePercentage = 10.0 / rete;
        this.sleepTime = (int) (time * 100 * retePercentage);
        this.rete = rete;
        this.screenEnum = screenEnum;
        log.info("sleepTime = {}, retePercentage = {}", sleepTime, retePercentage);
    }

    public CaptureScreenThread(int time, int rete) {
        this(time, rete, CaptureScreenEnum.SAVE_IMAGE);
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            return;
        }
        try {
            if (screenEnum == null || screenEnum == CaptureScreenEnum.SAVE_IMAGE) {
                ScreenImageInfo name = CaptureScreenUtils.createScreenImageFour();
                FileUpload.restTemplateTransferFilePost("http://127.0.0.1:12803/picture/upload", name.getPath(), name.getFileName(), null);
            } else if (screenEnum == CaptureScreenEnum.NO_SAVE_IMAGE) {
                ScreenImageInfo info = CaptureScreenUtils.createInputImage(0.4, 0.4);

                FileUpload.restTemplateTransferFilePost("http://127.0.0.1:12803/picture/upload", new MultipartFileImpl(info.getFileName(), (ByteArrayInputStream) info.getInputStream()), info.getFileName(), null);
            } else if (screenEnum == CaptureScreenEnum.BASE64_IMAGE){
                ScreenImageInfo info = CaptureScreenUtils.createOutputImage(0.4, 0.4);
                ImageInfo imageInfo = new ImageInfo();
                imageInfo.setImagePort(12802);
                imageInfo.setImageIp(InetAddress.getLocalHost().getHostName());
                String image = ImageUtil.getImageBase64ByOutStream((ByteArrayOutputStream)info.getOutputStream());
                imageInfo.setImageData(image);
                imageInfo.setImageName(info.getFileName());

                OKHttpUtils.postJSONStr("http://127.0.0.1:12803/picture/uploadImage", JSON.toJSONString(imageInfo));
            }

        } catch (IOException e) {
            log.error("截屏,线程 = {}， IOException", sleepTime, e);
        } catch (AWTException e) {
            log.error("截屏,线程 = {}， AWTException", sleepTime, e);
        }
        long endTime = System.currentTimeMillis();
        log.info("startTime = {}!startTime = {}", startTime, sleepTime);
        log.info("endTime = {}!", endTime);
    }
}
